/*
* ============================================================================
* 功能描述 ：应用层配置
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <stdio.h>
app_data_s app_data;

bit xx_f;

char js;
u8 Distribution_time = 0;			// 配网时间
u8 Distribution_chaxun = 0;			// 配网时间
u32 pei_time = 0;
bit san = 0;   // 配网闪
bit x = 0;

#if 0
static void _temp_handle(signed short t, signed short h)
{
	unsigned char t10 = 0, t100 = 0, t1000 = 0;
	unsigned char h10 = 0, h100 = 0, h1000 = 0;
	
	t10 	= (t/10%10);		// 十位位
	t100 	= (t/100%10);		// 百位
	t1000 	= (t/1000);			// 千位
	DelayXms(2);
//	t_10 	= _temp_lcd(t10);
//	t_100 	= _temp_lcd(t100);
//	t_1000 	= _temp_lcd(t1000);

	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	j  = j1*256+j2;

	h10		= (j/100%10); 		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10);		// wan位
	DelayXms(2);
//	h_10	= _temp_lcd(h10);
//	h_100	= _temp_lcd(h100);
//	h_1000	= _temp_lcd(h1000);

//	hp[0] = SHORT_GET_HIGH_BYTE(h_10);
//	hp[1] = SHORT_GET_LOW_BYTE(h_10);
//	hp[2] = SHORT_GET_HIGH_BYTE(h_100);
//	hp[3] = SHORT_GET_LOW_BYTE(h_100);
//	hp[4] = SHORT_GET_HIGH_BYTE(h_1000);
//	hp[5] = SHORT_GET_LOW_BYTE(h_1000);
	if(t1000 == 1)
	{
		if(t100 <= 8 )
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else if(t1000 == 2)
	{
		if(t100 <= 8)
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else
		Write_Lcd_Storage(face_state,0);
	
#if 0
	char a1[2],a2[2],a3[2];
	a1[0] = h10;
	a2[0] = h100;
	a3[0] = h1000;
	DelayXms(200);
	Uart_Tx_buf(a1, 1);		// 打印每一位值
	DelayXms(200);
	Uart_Tx_buf(a2, 1);
	DelayXms(200);
	Uart_Tx_buf(a3, 1);
#endif
	//Lcd_Set_temp(t_1000, t_100, t_10);
	Write_Lcd_Storage(temp_1,t10);
	DelayXms(2);
	Write_Lcd_Storage(temp_10,t100);
	DelayXms(2);
	Write_Lcd_Storage(temp_100,t1000);
	DelayXms(2);
	Write_Lcd_Storage(hum_10,h100);
	DelayXms(2);
	Write_Lcd_Storage(hum_100,h1000);
	Seg_Set_Data();
}
#endif

void App_Run(void)
{
	Zigbee_Cmd_handle();
		if(zigbee_app.zigbee_ready_flag == 1)
		{
			Zigbee_wake_up();
			DelayXms(15);
			Hly_Packet_device_manage(set_device_type, 2, DEV_ZIGBEE_WHT, 23);
			DelayXms(20);
			Hly_Packet_device_manage(set_number_of_endpoints, 1, zigbee_app.ed_Endpoint, 22);
			DelayXms(20); 
			Hly_Packet_device_manage(gujian_leix, 1, 0, 3);
			DelayXms(20); 
			Hly_Packet_device_manage(start_Protocol_stack, 0, 0, 0);
			DelayXms(20);
			Hly_Packet_ModelId();
			DelayXms(20);
			Hly_Packet_manufacturername();
			DelayXms(20);
			Hly_Packet_Network_msg(query_current_network_status, 0, 0);
					
			Zigbee_sleep();
			xx_f = 1;
			zigbee_app.zigbee_ready_flag = 0;

		}
	
		if(zigbee_app.network_status == 0x00 || zigbee_app.network_status == 0x02)
		{
		
			x = 0;
			key_on();
			if(js == 0 || zigbee_app.zigbee_ready_flag == 1)
			{
				
				js = 1;
	
			}
		}
		if(zigbee_app.network_status == 0x01)
		{
			if (x == 0) 
			{
				Zigbee_wake_up();
				DelayXms(15);
				hly_Obtain_voltage();
		   //	  Hly_Temp_Hum_Obtain();
				Hly_tuya_updata(Report_specified_type, app_data.power_supply);
			   RB5 = 1;
				x = 1;
				
				Zigbee_sleep();
			}
			key_on();
	
		}
		if(zigbee_app.network_status == 0x03)
		{
				
			while(zigbee_app.network_status == 0x03)
			{
			
				asm("clrwdt");	// 喂狗
				Zigbee_Cmd_handle();			// 串口数据接收
				js = 0;
				x = 0;
				
				if(hly_get_elapsed_ms(pei_time) >= 5)
				{
					pei_time = get_current_ms();					
					if(san == 0)  
					{
						Zigbee_led_on();			
						san = 1;
					}
					else if(san == 1)
					{
						Zigbee_led_off();			
						san = 0;
						Distribution_time++;
						Distribution_chaxun++;
					}
	
				}
				if(Distribution_chaxun == 10)
				{
					Zigbee_wake_up();
					Distribution_chaxun = 0;
					DelayXms(15);
					Hly_Packet_Network_msg(query_current_network_status, 0, 0);
					
					Zigbee_sleep();
				}
	
				if(Distribution_time == 200)
				{
					Zigbee_wake_up();
					DelayXms(15);
					Hly_Packet_Network_msg(control_zigbee_status, 1, 2);
					zigbee_app.network_status = 0;
					Distribution_time = 0;
					Zigbee_sleep();
				}
	
			}
			
		}
	

}

void app_init(void)
{
	Hly_Packet_Network_msg(query_current_network_status, 0, 0);
	DelayXms(10);
//	Hly_Temp_Hum_Obtain();
}

